package org.ielts.service;

import java.util.Date;

import org.ielts.domain.base.Course;
import org.ielts.domain.base.SignCourse;
import org.ielts.domain.base.Student;
import org.ielts.domain.log.ChangeSignCourseLog;
import org.ielts.domain.log.SignCourseLog;
import org.ielts.domain.security.User;
import org.ielts.repository.ChangeSignCourseLogRepository;
import org.ielts.repository.SignCourseLogRepository;
import org.ielts.repository.SignCourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yifeng.ProfileContext;
import com.yifeng.profile.Profile;

@Service
public class SignCourseLogService {

	@Autowired
	SignCourseLogRepository signCourseLogRepository;

	@Autowired
	SignCourseRepository signCourseRepository;

	@Transactional
	public SignCourseLog findOne(String id) {
		SignCourseLog signCourseLog = signCourseLogRepository.findOne(id);

		return signCourseLog;
	}

	@Autowired
	ChangeSignCourseLogRepository changeSignCourseLogRepository;

	@Transactional
	public ChangeSignCourseLog changeSignCourse(String oldId, String title, int times, Date endDate, String contract) {
		SignCourse signCourse = signCourseRepository.findOne(oldId);

		ChangeSignCourseLog changeSignCourseLog = new ChangeSignCourseLog();
		changeSignCourseLog.setCreateTime(new Date());

		User user = (User) Profile.getInstance().getUser();

		changeSignCourseLog.setCreator(user);

		changeSignCourseLog.setNewEndDate(endDate);
		changeSignCourseLog.setNewTimes(times);
		changeSignCourseLog.setOldEndDate(signCourse.getEndTime());
		changeSignCourseLog.setOldTimes(signCourse.getTimes());

		changeSignCourseLog.setSignCourse(signCourse);

		changeSignCourseLog.setStudent(signCourse.getStudent());
		changeSignCourseLog.setTitle(title);

		changeSignCourseLogRepository.save(changeSignCourseLog);

		signCourse.setEndTime(endDate);
		signCourse.setTimes(times);
		signCourse.setContractId(contract);

		return changeSignCourseLog;
	}

	@Transactional
	public Page<SignCourseLog> findAll(Pageable pageable) {
		Page<SignCourseLog> signCourseLogs = signCourseLogRepository.findAll(pageable);

		return signCourseLogs;
	}

	@Transactional
	public SignCourseLog save(SignCourseLog signCourseLog) {
		return signCourseLogRepository.save(signCourseLog);
	}

	@Transactional
	public void update(SignCourseLog signCourseLog) {
		signCourseLogRepository.save(signCourseLog);
	}

	@Transactional
	public Page<SignCourseLog> search(String name, Boolean checked, String[] constansId, Pageable pageable) {
		Page<SignCourseLog> logs = new PageImpl<SignCourseLog>(signCourseLogRepository.search(name, checked,
				constansId, pageable), pageable, signCourseLogRepository.countSearch(name, checked, constansId));
		return logs;
	}
}
